package cn.e2win.biz.security;

import java.util.HashMap;
import java.util.Map;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;

import cn.e2win.base.entity.SysUser;

public class UserSession{

    private static final Map<Integer,SysUser> MAP = new HashMap<Integer,SysUser>(); 
	public static SysUser getUser() {
	    Authentication  authentication =  SecurityContextHolder.getContext().getAuthentication();
	    if(authentication==null){
	    	SysUser user = new SysUser();
	    	user.setCoId(-99);
	    	user.setId(-99);
	    	user.setLevelInt(4);
	    	
	    	return user;
	    }
	    SysUser user = (SysUser)authentication.getPrincipal();
	    //return user;
	    return MAP.get(user.getId());
	}
	public static void addUser(SysUser user){
		MAP.put(user.getId(), user);
	}
	public static SysUser getUser(Integer id){
		return MAP.get(id);
	}
	public static boolean isOnLine(Integer userId){
		return MAP.containsKey(userId);
	}
	public static void removeUser(Integer userId){
		MAP.remove(userId);
	}
}